package host.skiree.nettydemo.server.dto.impl;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import host.skiree.nettydemo.server.dto.UserChannel;
import io.netty.channel.Channel;


/**
 * @author gcl
 * @date 2022/9/20 8:46
 */
public class UserChannelImpl implements UserChannel {

    /**
     * 用户存储 用户信道和用户名 的对应关系 HashBiMap 线程不安全
     */
    public static BiMap<Channel ,String> USER_CHANNEL_MAP = HashBiMap.create();

    @Override
    public void bind(Channel channel, String userName) {
        USER_CHANNEL_MAP.put(channel, userName);
        System.out.println(USER_CHANNEL_MAP.toString());
    }

    @Override
    public void unbind(Channel channel) {
        USER_CHANNEL_MAP.remove(channel);
    }

    @Override
    public Channel getChannel(String userName) {
        return USER_CHANNEL_MAP.inverse().get(userName);
    }
}
